#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_constrain1b.info <<'%EOF%'
   geoopt_constrain1b
   ------------------
   Molecule:         C2H6
   Wave Function:    SCF / STO-3G
   Test Purpose:     Shows a constrained optimization of ethane, keeping the
                     C-C bond distance fixed (coordinate #1 as determined
                     from geoopt_constrain1a)
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_constrain1b.mol <<'%EOF%'
BASIS
STO-3G
Constrained geometry optimization of ethane

Atomtypes=2
Charge=6.0 Atoms=2
C     0.0000000000        0.0000000000        1.7547876282             *
C     0.0000000000        0.0000000000       -1.7547876282             *
Charge=1.0 Atoms=6
H     1.6808445513        0.9704360937        2.4445545504             *
H    -1.6808445513        0.9704360937        2.4445545504             *
H     0.0000000000       -1.9408721894        2.4445545504             *
H     1.6808445513       -0.9704360937       -2.4445545504             *
H    -1.6808445513       -0.9704360937       -2.4445545504             *
H     0.0000000000        1.9408721894       -2.4445545504             *
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_constrain1b.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
*OPTIMIZE
.CONSTR
1
1
**WAVE FUNCTION
.HF
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_constrain1b.check
cat >>geoopt_constrain1b.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

# Geometry optimization
CRIT1=`$GREP "Default 1st order method will be used\: * BFGS update\." $log | wc -l`
CRIT2=`$GREP "Optimization will be performed in redundant internal coordinates\." $log | wc -l`
CRIT3=`$GREP "Model Hessian will be used as initial Hessian\." $log | wc -l`
CRIT4=`$GREP "Trust region method will be used to control step \(default\)\." $log | wc -l`
CRIT5=`$GREP "Constrained optimization has been requested\." $log | wc -l`
CRIT6=`$GREP "The following coordinate numbers will be held fixed during the optimization\:" $log | wc -l`
CRIT7=`$GREP "Coordinate \# * 1" $log | wc -l`
TEST[1]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7`
CTRL[1]=6  # no redundant internal coordinates for diatomics any more
ERROR[1]="CONSTRAINED GEOMETRY OPTIMIZATION NOT SET UP CORRECTLY"

# Symmetry
CRIT1=`$GREP "Symmetry class found\: D\(3d\)" $log | wc -l`
CRIT2=`$GREP "The following symmetry elements were found\: * X * YZ" $log | wc -l`
CRIT3=`$GREP "Number of coordinates in each symmetry\: * 7 * 5 * 7 * 5" $log | wc -l`
CRIT4=`$GREP "Number of orbitals in each symmetry\: * 6 * 2 * 6 * 2" $log | wc -l`
TEST[2]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[2]=6
ERROR[2]="SYMMETRY NOT CORRECT"

# Geometry
CRIT1=`$GREP "Total number of coordinates\: * 24" $log | wc -l`
CRIT2=`$GREP "\:  * .[0-9] * x *\-*0*\.0000000000" $log | wc -l`
CRIT3=`$GREP "\:  * .[0-9] * x * \-*1\.6808445" $log | wc -l`
CRIT4=`$GREP "[0-9][0-9]  * .[0-9] * y *\-*0*\.0000000000" $log | wc -l`
CRIT5=`$GREP "[0-9][0-9]  * .[0-9] * y *\-*0*\.9704360" $log | wc -l`
CRIT6=`$GREP "[0-9][0-9]  * .[0-9] * y * \-*1\.9408721" $log | wc -l`
CRIT7=`$GREP "[0-9][0-9]  * .[0-9] * z * \-*1\.7547876" $log | wc -l`
CRIT8=`$GREP "[0-9][0-9]  * .[0-9] * z * \-*2\.4445545" $log | wc -l`
TEST[3]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8`
CTRL[3]=25
ERROR[3]="GEOMETRY NOT READ CORRECTLY"

# Initial energy
CRIT1=`$GREP "Final * HF energy\: * \-78\.2565905[567]" $log | wc -l`
TEST[4]=`expr	$CRIT1`
CTRL[4]=1
ERROR[4]="INITIAL ENERGY NOT CORRECT"

# Initial gradient
CRIT1=`$GREP "C * z *  0*\.24289" $log | wc -l`
CRIT2=`$GREP "C * y * \-0*\.00000" $log | wc -l`
CRIT3=`$GREP "H * z *  0*\.00954" $log | wc -l`
CRIT4=`$GREP "H * y *  0*\.00346" $log | wc -l`
CRIT5=`$GREP "H * z *  0*\.01908" $log | wc -l`
CRIT6=`$GREP "H * y * -0*\.00346" $log | wc -l`
CRIT7=`$GREP "H * x *  0*\.00600" $log | wc -l`
TEST[5]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7`
CTRL[5]=7
ERROR[5]="INITIAL GRADIENT NOT CORRECT"

# Initial step
#CRIT1=`$GREP "C * 2 * 0*\.00000000.* \-0*\.0000000.* \-1\.7547876" $log | wc -l`
CRIT1=1
#hjaaj: the carbon atom doesn't really move, so number of hits change easily
CRIT2=`$GREP "H * _. *  0*\.000000* * \-*1\.95943.*   2\.3722[56]" $log | wc -l`
CRIT3=`$GREP "H * _. *  0*\.000000* * \-*1\.95943.* \-2\.3722[56]" $log | wc -l`
CRIT4=`$GREP "H * _. *   1\.6969(19|20).* \-*0*\.97971.*   2\.3722[56]" $log | wc -l`
CRIT5=`$GREP "H * _. * \-1\.6969(19|20).* \-*0*\.97971.*   2\.3722[56]" $log | wc -l`
CRIT6=`$GREP "H * _. *   1\.6969(19|20).* \-*0*\.97971.* \-2\.3722[56]" $log | wc -l`
CRIT7=`$GREP "H * _. * \-1\.6969(19|20).* \-*0*\.97971.* \-2\.3722[56]" $log | wc -l`
TEST[6]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7`
CTRL[6]=7
ERROR[6]="INITIAL STEP NOT CORRECT"

# Second iteration
CRIT1=`$GREP "Energy at this geometry is * \: * \-78\.25702[0-9]" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * 0*\.1927" $log | wc -l`
CRIT3=`$GREP "Norm of step     * \: * 0*\.0459" $log | wc -l`
TEST[7]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[7]=3
ERROR[7]="SECOND ITERATION NOT CORRECT"

# Third iteration
CRIT1=`$GREP "Norm of gradient * \: * (0| )\.1877" $log | wc -l`
CRIT2=`$GREP "Norm of step     * \: * (0| )\.0014" $log | wc -l`
TEST[8]=`expr	$CRIT1 \+ $CRIT2`
CTRL[8]=2
ERROR[8]="THIRD ITERATION NOT CORRECT"

# Final geometry
CRIT1=`$GREP -l "C * _1 *  0*\.00000.* *\-*0*\.00000.*    *   1\.7547876282" $log | wc -l`
CRIT2=`$GREP -l "C * _2 *  0*\.00000.* *\-*0*\.00000.*    * \-1\.7547876282" $log | wc -l`
# many digits in z-coordinate in CRIT1 and CRIT2 because this coordinate
# is constrained to be fixed.
CRIT3=`$GREP -l "H * _. *  0*\.00000.* *  \-*1\.9483[89].* *   2\.3971(4|5)" $log | wc -l`
CRIT4=`$GREP -l "H * _. *  0*\.00000.* *  \-*1\.9483[89].* * \-2\.3971(4|5)" $log | wc -l`
CRIT5=`$GREP -l "H * _. *   1\.68735.* * \-*0*\.97419.*    *   2\.3971(4|5)" $log | wc -l`
CRIT6=`$GREP -l "H * _. * \-1\.68735.* * \-*0*\.97419.*    *   2\.3971(4|5)" $log | wc -l`
CRIT7=`$GREP -l "H * _. *   1\.68735.* * \-*0*\.97419.*    * \-2\.3971(4|5)" $log | wc -l`
CRIT8=`$GREP -l "H * _. * \-1\.68735.* * \-*0*\.97419.*    * \-2\.3971(4|5)" $log | wc -l`
TEST[9]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8`
CTRL[9]=8
ERROR[9]="FINAL GEOMETRY NOT CORRECT"

# Geometry convergence
CRIT1=`$GREP "Constrained optimization converged in * 4 * iterations\!" $log | wc -l`
CRIT2=`$GREP "Removing the constraint\(s\) might decrease the energy further\." $log | wc -l`
CRIT3=`$GREP "Energy at final geometry is     * \: * \-78\.25722[0-9] a\.u\." $log | wc -l`
CRIT4=`$GREP "Energy change during optimization \: * \-0*\.00063[0-9] a\.u\." $log | wc -l`
TEST[10]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[10]=4
ERROR[10]="GEOMETRY OPTIMIZATION NOT CONVERGED"

PASSED=1
for i in 1 2 3 4 5 6 7 8 9 10
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} : ${TEST[i]} .ne. ${CTRL[i]} ; "
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
